home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / comp.c < prev    next >
Text File  |  1985-11-13  |  3KB  |  117 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> COMP.C - COMPARE 2 FILES & LIST DIFFERENCES <--- */
  19.  
  20. /* 26 OCT 80: Adapt from WC version */
  21.  
  22. /***********************************************
  23.  
  24. usage: comp filea fileb [flags]
  25.  
  26. results in format: blank line/line #/filea line/fileb line
  27.  
  28. flags:     -a: omit filea line
  29.      -b: omit fileb line
  30.      -l omit blank line & line #
  31.      -s output if lines same instead of diff
  32.         (turns on -b)
  33.  
  34. ***********************************************/
  35.  
  36. #include <dmfio.h>
  37.  
  38. main (ac, av)
  39. int ac;
  40. char *av[];
  41. {
  42.    char *arg;                  /* pointo flag arg */
  43.    BOOL skipno, skipa, skipb, shosam;      /* option flags */
  44.    char iobufa[BUFSIZ], iobufb[BUFSIZ];   /* file buffers */
  45.    char lina[MAXLINE], linb[MAXLINE];      /* line buffers */
  46.    BOOL same;                  /* if lina==linb */
  47.    BOOL eofa, eofb;              /* wether EOF yet */
  48.    BOOL msga, msgb;              /* if EOF msg sent yet */
  49.    unsigned lines;              /* line counter */
  50.    char eoform[30];              /* format string 4 EOF msgs */
  51.  
  52.    skipno = skipa = skipb = shosam = eofa = eofb = msga = msgb = NO;
  53.    lines = 0;
  54.    strcpy(eoform, "\nEOF on %s after %u lines\n");
  55.  
  56.    if (ac < 3) {
  57.       puts("usage: comp fileA fileB [options]\n");
  58.       puts("options: -a: omit A lines; -b: omit B lines\n");
  59.       puts("         -l: omit line #s; -s: list A lines if A=B\n");
  60.       exit ();
  61.    }
  62.  
  63.    if (fopen(av[1], iobufa) == ERROR)
  64.       errxit("File A I/O Error");
  65.    if (fopen(av[2], iobufb) == ERROR)
  66.       errxit("File B I/O Error");
  67.  
  68.    while (ac > 3 && *(arg = av[--ac]) == '-')
  69.       switch (toupper(*++arg)) {
  70.      case 'A': skipa = YES; break;
  71.      case 'S': shosam = YES;
  72.      case 'B': skipb = YES; break;
  73.      case 'L': skipno = YES; break;
  74.       default: errxit("Bad Flag");
  75.       }
  76.  
  77. /*
  78.      >>------> MAIN LOOP <------<<
  79. */
  80.  
  81.    while (!(eofa && eofb)) {
  82.       if (!eofa)
  83.      if (fgets(lina, iobufa) == NULL) eofa = YES;
  84.       if (!eofb)
  85.      if (fgets(linb, iobufb) == NULL) eofb = YES;
  86.       if (eofa && eofb)
  87.      break;
  88.  
  89.       ++lines;
  90.  
  91.       same = strcmp(lina, linb) == 0;
  92.       if (shosam == same) {
  93.      if (!skipno) printf("\n--> %u\n", lines);
  94.      if (!skipa && !eofa) puts(lina);
  95.      if (!skipb && !eofb) puts(linb);
  96.       }
  97.  
  98.       if (eofa && !msga) {
  99.      msga = YES;
  100.      printf(eoform, "File A", lines - 1);
  101.       }
  102.       if (eofb && !msgb) {
  103.      msgb = YES;
  104.      printf(eoform, "File B", lines - 1);
  105.       }
  106.    }
  107.  
  108.    if (msga)
  109.       printf(eoform, "File B", lines);
  110.    else if (msgb)
  111.       printf(eoform, "File A", lines);
  112.    else
  113.       printf(eoform, "Both Files", lines);
  114.  
  115.    fclose(iobufa); fclose(iobufb);
  116. }
  117.